package com.xxl.rpc.admin.controller;

import com.xxl.rpc.admin.controller.annotation.PermessionLimit;
import com.xxl.rpc.admin.controller.interceptor.PermissionInterceptor;
import com.xxl.rpc.admin.core.result.ReturnT;
import com.xxl.rpc.admin.dao.IXxlRpcRegistryDao;
import com.xxl.rpc.admin.dao.IXxlRpcRegistryDataDao;
import com.xxl.rpc.admin.test.T15_FullGC_Problem01;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * index controller
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
public class IndexController {


	@Resource
	private IXxlRpcRegistryDao xxlRpcRegistryDao;
	@Resource
	private IXxlRpcRegistryDataDao xxlRpcRegistryDataDao;



	@RequestMapping("/")
	public String index(Model model, HttpServletRequest request) {

		int registryNum = xxlRpcRegistryDao.pageListCount(0, 1, null, null, null);
		int registryDataNum = xxlRpcRegistryDataDao.count();

		model.addAttribute("registryNum", registryNum);
		model.addAttribute("registryDataNum", registryDataNum);

		return "index";
	}

	@RequestMapping("/toLogin")
	@PermessionLimit(limit=false)
	public String toLogin(Model model, HttpServletRequest request) {
		if (PermissionInterceptor.ifLogin(request)) {
			return "redirect:/";
		}
		return "login";
	}

	@RequestMapping(value="login", method=RequestMethod.POST)
	@ResponseBody
	@PermessionLimit(limit=false)
	public ReturnT<String> loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password, String ifRemember){
		// valid
		if (PermissionInterceptor.ifLogin(request)) {
			return ReturnT.SUCCESS;
		}

		// param
		if (userName==null || userName.trim().length()==0 || password==null || password.trim().length()==0){
			return new ReturnT<String>(500, "请输入账号密码");
		}
		boolean ifRem = (ifRemember!=null && "on".equals(ifRemember))?true:false;

		// do login
		boolean loginRet = PermissionInterceptor.login(response, userName, password, ifRem);
		if (!loginRet) {
			return new ReturnT<String>(500, "账号密码错误");
		}
		return ReturnT.SUCCESS;
	}

	@RequestMapping(value="logout", method=RequestMethod.POST)
	@ResponseBody
	@PermessionLimit(limit=false)
	public ReturnT<String> logout(HttpServletRequest request, HttpServletResponse response){
		if (PermissionInterceptor.ifLogin(request)) {
			PermissionInterceptor.logout(request, response);
		}
		return ReturnT.SUCCESS;
	}
	
	@RequestMapping("/help")
	public String help() {
		return "help";
	}


	public int minPathSum(int[][] grid) {
		int m = grid.length;
		int n = grid[0].length;
		int[][] dp =new int[m][n];
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];
			}
		}
		return dp[m-1][n-1];
	}

	public static boolean isPalindrome(ListNode head) {
		if(head.next==null||head.next==null){
			return true;
		}
		ListNode slow =head;
		ListNode fast =head;
		while(fast.next!=null&&fast.next.next!=null){
			fast=fast.next.next;
			slow=slow.next;
		}
		ListNode secondNode =slow.next;

		return equalsFlag(head,reverse(secondNode));
	}

	static Boolean equalsFlag(ListNode headA,ListNode headB){
		while(headA!=null&&headB!=null){
			if(headA.val!=headB.val){
				return false;
			}
			headA=headA.next;
			headB=headB.next;
		}
		return true;
	}

	static ListNode reverse(ListNode head){
		ListNode pre = null;
		ListNode curr = head;
		while(curr!=null){
			ListNode tmp=curr.next;
			curr.next=pre;
			//右移
			pre=curr;
			curr=tmp;
		}
		return pre;
	}


	public int longSubs(String s){
		Map<Character,Integer > map = new HashMap<>();//map的key 是字符 value 是字符的坐标+1
		int max=0;
		int length=s.length();
		for(int end=0, start=0;end<length;end++){
			char value =s.charAt(end);
			if (map.containsKey(value)){
				start=Math.max(start,map.get(value)+1);//
			}
			max=Math.max(max,end-start+1);
			map.put(value,end);//
		}
		return max;
	}

 public static class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
	public static ListNode reverseList(ListNode head) {
		ListNode prev = head;
		ListNode curr = head.next;
		while (curr != null) {
			prev.next=null;
			ListNode next = curr.next;
			curr.next = prev;
			prev = curr;
			curr = next;
		}
		return prev;
	}

	public static void main(String[] args) {
		ListNode listNode1 = new ListNode(1);
		ListNode listNode2 = new ListNode(2);
		ListNode listNode3 = new ListNode(2);
		ListNode listNode4 = new ListNode(1);
		listNode1.next=listNode2;
		listNode2.next=listNode3;listNode3.next=listNode4;
		Boolean res =isPalindrome(listNode1);
		System.out.println(res);
	}

	@RequestMapping("/test")
	@ResponseBody
	public String test() throws Exception {
		T15_FullGC_Problem01.test();
		return "test";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
